<template>
  <div class="panel site-overview-panel">
    <div class="panel-header">
      <i class="fas fa-chart-line" />
      <span>站点概览</span>
      <div class="refresh-btn" @click="refreshData">
        <el-icon v-if="isRefreshing" class="rotating">
          <Refresh />
        </el-icon>
        <i v-else class="fas fa-sync-alt" />
      </div>
    </div>

    <!-- 核心统计 -->
    <div class="core-stats">
      <div class="stat-card primary">
        <div class="stat-icon">
          <i class="fas fa-users" />
        </div>
        <div class="stat-content">
          <div class="stat-value">{{ formatNumber(stats.userTotal) }}</div>
          <div class="stat-label">总用户</div>
          <div class="stat-trend" :class="{ 'positive': stats.userTrend > 0 }">
            <i :class="stats.userTrend > 0 ? 'fas fa-arrow-up' : 'fas fa-arrow-down'" />
            {{ Math.abs(stats.userTrend) }}%
          </div>
        </div>
      </div>

      <div class="stat-card success">
        <div class="stat-icon">
          <i class="fas fa-file-alt" />
        </div>
        <div class="stat-content">
          <div class="stat-value">{{ formatNumber(stats.articleTotal) }}</div>
          <div class="stat-label">文章数</div>
          <div class="stat-trend" :class="{ 'positive': stats.articleTrend > 0 }">
            <i :class="stats.articleTrend > 0 ? 'fas fa-arrow-up' : 'fas fa-arrow-down'" />
            {{ Math.abs(stats.articleTrend) }}%
          </div>
        </div>
      </div>

      <div class="stat-card info">
        <div class="stat-icon">
          <i class="fas fa-eye" />
        </div>
        <div class="stat-content">
          <div class="stat-value">{{ formatNumber(stats.viewTotal) }}</div>
          <div class="stat-label">总访问</div>
          <div class="stat-trend" :class="{ 'positive': stats.viewTrend > 0 }">
            <i :class="stats.viewTrend > 0 ? 'fas fa-arrow-up' : 'fas fa-arrow-down'" />
            {{ Math.abs(stats.viewTrend) }}%
          </div>
        </div>
      </div>
    </div>

    <!-- 今日数据 -->
    <div class="today-stats">
      <div class="section-title">
        <i class="fas fa-calendar-day" />
        今日数据
      </div>
      <div class="today-grid">
        <div class="today-item">
          <div class="today-value">{{ stats.todayViews }}</div>
          <div class="today-label">访问量</div>
        </div>
        <div class="today-item">
          <div class="today-value">{{ stats.todayUsers }}</div>
          <div class="today-label">新用户</div>
        </div>
        <div class="today-item">
          <div class="today-value">{{ stats.todayComments }}</div>
          <div class="today-label">评论数</div>
        </div>
        <div class="today-item">
          <div class="today-value">{{ stats.todayLikes }}</div>
          <div class="today-label">点赞数</div>
        </div>
      </div>
    </div>

    <!-- 实时状态 -->
    <div class="realtime-status">
      <div class="section-title">
        <i class="fas fa-broadcast-tower" />
        实时状态
      </div>
      <div class="status-grid">
        <div class="status-item">
          <div class="status-indicator online"></div>
          <div class="status-content">
            <div class="status-value">{{ stats.onlineUsers }}</div>
            <div class="status-label">在线用户</div>
          </div>
        </div>
        <div class="status-item">
          <div class="status-indicator active"></div>
          <div class="status-content">
            <div class="status-value">{{ stats.activeUsers }}</div>
            <div class="status-label">活跃用户</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 趋势图表 -->
    <div class="trend-chart">
      <div class="section-title">
        <i class="fas fa-chart-area" />
        7天访问趋势
      </div>
      <div class="chart-container">
        <div class="chart-bars">
          <div
            v-for="(day, index) in stats.weeklyTrend"
            :key="index"
            class="chart-bar"
            :style="{ height: `${(day.value / Math.max(...stats.weeklyTrend.map(d => d.value))) * 100}%` }"
            :title="`${day.date}: ${day.value}`"
          >
            <div class="bar-value">{{ day.value }}</div>
          </div>
        </div>
        <div class="chart-labels">
          <span
            v-for="(day, index) in stats.weeklyTrend"
            :key="index"
            class="chart-label"
          >
            {{ day.label }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Refresh } from '@element-plus/icons-vue';
import { ref, onMounted, onUnmounted } from 'vue';

const isRefreshing = ref(false);

// 站点统计数据
const stats = ref({
  // 核心统计
  userTotal: 15234,
  userTrend: 12.5,
  articleTotal: 876,
  articleTrend: 8.3,
  viewTotal: 234567,
  viewTrend: 15.7,

  // 今日数据
  todayViews: 1234,
  todayUsers: 56,
  todayComments: 23,
  todayLikes: 89,

  // 实时状态
  onlineUsers: 45,
  activeUsers: 128,

  // 7天趋势
  weeklyTrend: [
    { date: '周一', label: '一', value: 856 },
    { date: '周二', label: '二', value: 1234 },
    { date: '周三', label: '三', value: 987 },
    { date: '周四', label: '四', value: 1456 },
    { date: '周五', label: '五', value: 1678 },
    { date: '周六', label: '六', value: 1234 },
    { date: '周日', label: '日', value: 1567 }
  ]
});

// 格式化数字
const formatNumber = (num: number) => {
  if (num >= 1000000) {
    return (num / 1000000).toFixed(1) + 'M';
  } else if (num >= 10000) {
    return (num / 10000).toFixed(1) + 'W';
  } else if (num >= 1000) {
    return (num / 1000).toFixed(1) + 'K';
  }
  return num.toString();
};

// 刷新数据
const refreshData = async () => {
  isRefreshing.value = true;

  // 模拟API调用
  await new Promise(resolve => setTimeout(resolve, 1000));

  // 模拟数据更新
  stats.value.todayViews = Math.floor(Math.random() * 2000) + 1000;
  stats.value.todayUsers = Math.floor(Math.random() * 100) + 50;
  stats.value.onlineUsers = Math.floor(Math.random() * 80) + 30;
  stats.value.activeUsers = Math.floor(Math.random() * 200) + 100;

  isRefreshing.value = false;
};

let refreshTimer: NodeJS.Timeout | null = null;

onMounted(() => {
  // 每30秒自动刷新一次实时数据
  refreshTimer = setInterval(() => {
    stats.value.onlineUsers = Math.floor(Math.random() * 80) + 30;
    stats.value.activeUsers = Math.floor(Math.random() * 200) + 100;
  }, 30000);
});

onUnmounted(() => {
  if (refreshTimer) {
    clearInterval(refreshTimer);
  }
});
</script>

<style scoped lang="scss">
.panel {
  background: white;
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  padding: 16px;
}

.panel-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 16px;
  font-size: 1rem;
  position: relative;

  i {
    color: #6366f1;
    font-size: 1.1rem;
  }

  .refresh-btn {
    margin-left: auto;
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    transition: all 0.2s ease;
    color: #64748b;

    &:hover {
      background: #f1f5f9;
      color: #6366f1;
    }

    .rotating {
      animation: rotate 1s linear infinite;
    }
  }
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

// 核心统计卡片
.core-stats {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--card-bg) 0%, var(--card-bg-end) 100%);
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 60px;
    height: 60px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    transform: translate(20px, -20px);
  }

  &.primary {
    --card-bg: #6366f1;
    --card-bg-end: #8b5cf6;
  }

  &.success {
    --card-bg: #10b981;
    --card-bg-end: #34d399;
  }

  &.info {
    --card-bg: #f59e0b;
    --card-bg-end: #fbbf24;
  }
}

.stat-icon {
  flex-shrink: 0;
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 1.2rem;
}

.stat-content {
  flex: 1;
  color: white;

  .stat-value {
    font-size: 1.4rem;
    font-weight: 700;
    margin-bottom: 2px;
  }

  .stat-label {
    font-size: 0.8rem;
    opacity: 0.9;
    margin-bottom: 4px;
  }

  .stat-trend {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.75rem;
    opacity: 0.9;

    &.positive {
      color: #86efac;
    }

    &:not(.positive) {
      color: #fca5a5;
    }

    i {
      font-size: 0.7rem;
    }
  }
}

// 今日数据
.today-stats {
  margin-bottom: 16px;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.9rem;
  font-weight: 600;
  color: #475569;
  margin-bottom: 10px;

  i {
    font-size: 0.8rem;
    color: #6366f1;
  }
}

.today-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.today-item {
  padding: 12px;
  background: #f8fafc;
  border-radius: 8px;
  text-align: center;
  transition: all 0.2s ease;

  &:hover {
    background: #e2e8f0;
    transform: translateY(-1px);
  }

  .today-value {
    font-size: 1.1rem;
    font-weight: 700;
    color: #1e293b;
    margin-bottom: 2px;
  }

  .today-label {
    font-size: 0.75rem;
    color: #64748b;
  }
}

// 实时状态
.realtime-status {
  margin-bottom: 16px;
}

.status-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.status-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  background: #f8fafc;
  border-radius: 8px;
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  position: relative;

  &.online {
    background: #10b981;
    animation: pulse 2s infinite;
  }

  &.active {
    background: #f59e0b;
    animation: pulse 2s infinite;
  }
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7);
  }
  70% {
    box-shadow: 0 0 0 10px rgba(16, 185, 129, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(16, 185, 129, 0);
  }
}

.status-content {
  .status-value {
    font-size: 1rem;
    font-weight: 600;
    color: #1e293b;
    margin-bottom: 2px;
  }

  .status-label {
    font-size: 0.75rem;
    color: #64748b;
  }
}

// 趋势图表
.trend-chart {
  .chart-container {
    .chart-bars {
      display: flex;
      align-items: flex-end;
      justify-content: space-between;
      height: 80px;
      margin-bottom: 8px;
      gap: 4px;
    }

    .chart-bar {
      flex: 1;
      background: linear-gradient(to top, #6366f1, #8b5cf6);
      border-radius: 4px 4px 0 0;
      min-height: 8px;
      position: relative;
      cursor: pointer;
      transition: all 0.2s ease;

      &:hover {
        opacity: 0.8;
      }

      .bar-value {
        position: absolute;
        top: -20px;
        left: 50%;
        transform: translateX(-50%);
        font-size: 0.7rem;
        font-weight: 600;
        color: #64748b;
        opacity: 0;
        transition: opacity 0.2s ease;
      }

      &:hover .bar-value {
        opacity: 1;
      }
    }

    .chart-labels {
      display: flex;
      justify-content: space-between;
      font-size: 0.7rem;
      color: #94a3b8;
    }
  }
}
</style>